package com.uzhie.web.controller.websocket;

import com.alibaba.fastjson.JSONObject;
import com.uzhie.web.anno.Auth;
import com.uzhie.web.anno.SocketController;
import com.uzhie.web.anno.SocketMapping;
import com.uzhie.web.bean.PageBean;
import com.uzhie.web.bean.PayloadBean;
import com.uzhie.jt808.service.JT808Service;
import com.uzhie.web.service.DemoService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * Created by TangFei on 2018/7/18.
 * controller层统一接收 <strong>requestBody</strong> <strong>jsonObject</strong> 统一返回 <strong>responseBody</strong>
 */
@SocketController
public class DemoSocketController {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired(required = false)
    private DemoService service;

    @Autowired(required = false)
    private JT808Service jt808Service;

    @Auth(tokenRequired = false)
    @SocketMapping(value = "testMethod")
    public PayloadBean test(@RequestBody JSONObject body, @RequestBody PageBean page) throws InstantiationException, IllegalAccessException {
        logger.info(body);
        return null;
    }

    @Auth(tokenRequired = false)
    @SocketMapping(value = "test2Method")
    public PayloadBean test2(@RequestBody JSONObject body, @RequestBody PageBean page) throws InstantiationException, IllegalAccessException {
        logger.info(body);
        throw new RuntimeException("test2 异常了");
    }

    @Auth(tokenRequired = false)
    @SocketMapping(value = "test3Method")
    public PayloadBean test3(@RequestBody JSONObject body) throws InstantiationException, IllegalAccessException {
        logger.info(body);
        // 下发808指令
        byte[] bytes = new byte[1];
        bytes[0] = (byte) (body.getIntValue("obody") & 0XFF);
        jt808Service.post(0x8801, body.getString("simNum"), bytes);
        return null;
    }

}
